<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="HA集群," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HA 即 （high available）高可用，又被叫做双机热备，用于关键性业务。 简单理解就是，有两台机器A和B，正常是A提供服务，B待命闲置，当A宕机或服务宕掉，会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived，其中keepalived有负载均衡的功能">
<meta name="keywords" content="HA集群">
<meta property="og:type" content="article">
<meta property="og:title" content="HA 集群配置">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/HA集群/1. HA 集群配置/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HA 即 （high available）高可用，又被叫做双机热备，用于关键性业务。 简单理解就是，有两台机器A和B，正常是A提供服务，B待命闲置，当A宕机或服务宕掉，会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived，其中keepalived有负载均衡的功能">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/01.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/04.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/05.png?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:49:35.868Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HA 集群配置">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HA 即 （high available）高可用，又被叫做双机热备，用于关键性业务。 简单理解就是，有两台机器A和B，正常是A提供服务，B待命闲置，当A宕机或服务宕掉，会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived，其中keepalived有负载均衡的功能">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/01.jpeg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/HA集群/1. HA 集群配置/"/>





  <title>HA 集群配置 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/HA集群/1. HA 集群配置/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">HA 集群配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HA 即 （high available）高可用，又被叫做双机热备，用于关键性业务。 简单理解就是，有两台机器A和B，正常是A提供服务，B待命闲置，当A宕机或服务宕掉，会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived，其中keepalived有负载均衡的功能。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/01.jpeg?raw=true" alt="images"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如图所示为一个 HA 结构，一个交换机下面有两台机器 web1 和 web2 ，其中 web1 为主节点，正常是它在提供服务，而 web2 备用节点是闲置的。 web1 和 web2 中间有一根心跳线，检查对方的存活状态。流动 IP ，也叫 vip 是对外提供服务的 ip ，正常情况下，是配置在 web1 上的，当 web1 宕机后， web2 会自动配置该 vip ，对外提供服务。</p>
<h2 id="heartbeat-部署"><a href="#heartbeat-部署" class="headerlink" title="heartbeat 部署"></a>heartbeat 部署</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面使用 heartbeat 来做 HA 集群，并且把 nginx 服务作为 HA 对应的服务</p>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台机器，都是 centos6.5，网卡 eth0 ip 地址为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">master 192.168.0.69</div><div class="line">slave 192.168.0.68</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth1 ip 地址为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">master 192.168.91.100</div><div class="line">slave 192.168.91.101</div></pre></td></tr></table></figure>
<h4 id="1-hostname-设置好，分别为-master-和-slave"><a href="#1-hostname-设置好，分别为-master-和-slave" class="headerlink" title="1. hostname 设置好，分别为 master 和 slave"></a>1. hostname 设置好，分别为 master 和 slave</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上设置 hostname</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@HA1 ~]<span class="comment"># hostname master</span></div><div class="line">[root@HA1 ~]<span class="comment"># bash</span></div><div class="line">[root@master ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">NETWORKING=yes</div><div class="line">HOSTNAME=master</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上设置 hostname</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@HA2 ~]<span class="comment"># hostname slave</span></div><div class="line">[root@HA2 ~]<span class="comment"># bash</span></div><div class="line">[root@slave ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">NETWORKING=yes</div><div class="line">HOSTNAME=master</div></pre></td></tr></table></figure>
<h4 id="2-关闭防火墙"><a href="#2-关闭防火墙" class="headerlink" title="2.关闭防火墙"></a>2.关闭防火墙</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从上都操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">iptables -F</div><div class="line">service iptables save</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都关闭 selinux</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">setenforce 0</div><div class="line">sed -i <span class="string">'s/SELINUX=enforcing/SELINUX=disabled/'</span> /etc/selinux/config</div></pre></td></tr></table></figure>
<h4 id="3-配置-hosts"><a href="#3-配置-hosts" class="headerlink" title="3.配置 hosts"></a>3.配置 hosts</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/hosts</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">192.168.0.69 master</div><div class="line">192.168.0.68 slave</div></pre></td></tr></table></figure>
<h4 id="4-安装-epel-扩展源"><a href="#4-安装-epel-扩展源" class="headerlink" title="4.安装 epel 扩展源"></a>4.安装 epel 扩展源</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y epel-release</div></pre></td></tr></table></figure>
<h4 id="5-安装-heartbeat"><a href="#5-安装-heartbeat" class="headerlink" title="5.安装 heartbeat"></a>5.安装 heartbeat</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y heartbeat* libnet nginx</div></pre></td></tr></table></figure>
<h4 id="6-主上（master）配置"><a href="#6-主上（master）配置" class="headerlink" title="6.主上（master）配置"></a>6.主上（master）配置</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># cd /usr/share/doc/heartbeat-3.0.4/</span></div><div class="line">[root@master heartbeat-3.0.4]<span class="comment"># cp authkeys ha.cf haresources /etc/ha.d</span></div><div class="line">[root@master heartbeat-3.0.4]<span class="comment"># cd /etc/ha.d</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># vim authkeys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#auth 1</span></div><div class="line"><span class="comment">#1 crc</span></div><div class="line"><span class="comment">#2 shal HI!</span></div><div class="line"><span class="comment">#3 md5 Hello!</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#auth 3</span></div><div class="line"><span class="comment">#1 crc</span></div><div class="line"><span class="comment">#2 shal HI!</span></div><div class="line">3 md5 Hello!</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是用来验证的 crc 最简单， shal 最复杂。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># chmod 600 authkeys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 haresources 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># vim haresources</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment">#nodel 10.0.0.170 Filesystem::/dev/sda1::data1:;ext2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#</span></div><div class="line">master 192.168.0.70/24/eth0:0 nginx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：master 为主节点 hostname ，192.168.0.70 为 vip ，/24 为24网段，eth0:0 为 vip 的设备名，nginx 为 heartbeat 监控的服务，也是两台机器对外提供的核心服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑 ha.cf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;清空 ha.cf</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># &gt;ha.cf</span></div><div class="line">[root@master ha.d]<span class="comment"># vim ha.cf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">debugfile /var/<span class="built_in">log</span>/ha-debug</div><div class="line">logfile /var/<span class="built_in">log</span>/ha-log</div><div class="line">logfacility local0</div><div class="line">keepalive 2</div><div class="line">deadtime 30</div><div class="line">warntime 10</div><div class="line">initdead 60</div><div class="line">udpport 694</div><div class="line">ucast eth1 192.168.91.101</div><div class="line">auto_failback on</div><div class="line">node master</div><div class="line">node slave</div><div class="line">ping 192.168.91.1</div><div class="line">respawn hacluster /usr/lib64/heartbeat/ipfail</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置说明：</p>
<ul>
<li><code>debugfile /var/log/ha-debug</code>：该文件保存 heartbeat 的调试信息;</li>
<li><code>logfile /var/log/ha-log</code>：heartbeat 的日志文件；</li>
<li><code>keepalive 2</code>：心跳的时间间隔，默认时间单位为秒；</li>
<li><code>deadtime 30</code>：超出该时间间隔未收到对方节点的心跳，则认为对方已经死亡；</li>
<li><code>warntime 10</code>：超出该时间间隔未收到对方节点的心跳，则发出警告并记录到日志中；</li>
<li><code>initdead 60</code>：在某些系统上，系统启动或重启之后需要经过一段时间网络才能正常工作，该选项用于解决这种情况产生的时间间隔。取值至少为 deadtime 的两倍；</li>
<li><code>udpport 694</code>：设置广播通信使用的端口， 694 为默认使用的端口；</li>
<li><code>ucast eth1 192.168.91.101</code>：设置为对方机器心跳检测的网卡和 ip；</li>
<li><code>auto_failback on</code>：heartbeat 的两台机器分别为主节点和从节点。主节点在正常情况下占用资源并运行所有的服务，遇到故障时把资源交给从节点并由从节点运行服务。在该选项为 on 的情况下，一旦主节点恢复运行，则自动获取资源并取代从节点，负责不取代从节点；</li>
<li><code>node</code>： 指定主和从，各占一行，主在上从在下；</li>
<li><code>respawn hacluster /usr/lib/heartbeat/ipfail</code>：指定与 heartbeat 一同启动和关闭的进程，该进程被自动监视，遇到故障则重新启动。最常用的进程是 ipfail ，该进程用于检测和处理网络故障，需要配合 ping 语句指定的 ping node 来检测网络连接。如果系统是64，注意该文件的路径<code>/usr/lib64/heartbeat/ipfail</code>。</li>
</ul>
<h4 id="7-把主上的三个配置文件拷贝到从上"><a href="#7-把主上的三个配置文件拷贝到从上" class="headerlink" title="7.把主上的三个配置文件拷贝到从上"></a>7.把主上的三个配置文件拷贝到从上</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># cd /etc/ha.d/</span></div><div class="line">[root@master ha.d]<span class="comment"># scp authkeys ha.cf haresources slave:/etc/ha.d/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;scp 命令安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install openssh-clients</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果提示错误，主从都安装</p>
<h4 id="8-从上（slave）编辑-ha-cf"><a href="#8-从上（slave）编辑-ha-cf" class="headerlink" title="8.从上（slave）编辑 ha.cf"></a>8.从上（slave）编辑 ha.cf</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># vim /etc/ha.d/ha.cf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ucast eth1 192.168.91.101 修改为 ucast eth1 192.168.91.100</p>
<h4 id="9-启动-heartbeat"><a href="#9-启动-heartbeat" class="headerlink" title="9.启动 heartbeat"></a>9.启动 heartbeat</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先主，后从</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service heartbeat start</span></div></pre></td></tr></table></figure>
<h4 id="10-检查测试"><a href="#10-检查测试" class="headerlink" title="10.检查测试"></a>10.检查测试</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ifconfig</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 eth0:0</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/02.png?raw=true" alt="images"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ps aux | grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 nginx 进程</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/03.png?raw=true" alt="images"></p>
<h4 id="11-测试1"><a href="#11-测试1" class="headerlink" title="11.测试1"></a>11.测试1</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上故意禁 ping </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># iptables -I INPUT -p icmp -j DROP</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># ifconfig</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 eth0:0</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/04.png?raw=true" alt="images"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># ps aux | grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 nginx 进程</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/05.png?raw=true" alt="images"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># iptables -D INPUT -p icmp -j DROP</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上恢复 eth0:0 和 nginx，从上停止 eth0:0 和 nginx 服务</p>
<h4 id="12-测试2"><a href="#12-测试2" class="headerlink" title="12.测试2"></a>12.测试2</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上停止 heartbeat 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># service heartbeat stop</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上会启动 eth0:0 和 nginx 服务</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上开启 heartbeat 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># service heartbeat start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上恢复 eth0:0 和 nginx 服务，从上停止 eth0:0 和 nginx 服务</p>
<h4 id="13-测试脑裂"><a href="#13-测试脑裂" class="headerlink" title="13.测试脑裂"></a>13.测试脑裂</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从上都 down 掉 eth1 网卡</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ifdown eth1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都会启动 eth0:0 网卡和 nginx 服务</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/HA集群/" rel="tag"># HA集群</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Haproxy/6. 源码编译安装 haproxy/" rel="next" title="haproxy 源码编译安装">
                <i class="fa fa-chevron-left"></i> haproxy 源码编译安装
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/HA集群/2. heartbeat 配置文件详解/" rel="prev" title="heartbeat 部署过程">
                heartbeat 部署过程 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#heartbeat-部署"><span class="nav-number">1.</span> <span class="nav-text">heartbeat 部署</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备工作"><span class="nav-number">1.1.</span> <span class="nav-text">准备工作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-hostname-设置好，分别为-master-和-slave"><span class="nav-number">1.1.1.</span> <span class="nav-text">1. hostname 设置好，分别为 master 和 slave</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-关闭防火墙"><span class="nav-number">1.1.2.</span> <span class="nav-text">2.关闭防火墙</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-配置-hosts"><span class="nav-number">1.1.3.</span> <span class="nav-text">3.配置 hosts</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-安装-epel-扩展源"><span class="nav-number">1.1.4.</span> <span class="nav-text">4.安装 epel 扩展源</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-安装-heartbeat"><span class="nav-number">1.1.5.</span> <span class="nav-text">5.安装 heartbeat</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-主上（master）配置"><span class="nav-number">1.1.6.</span> <span class="nav-text">6.主上（master）配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-把主上的三个配置文件拷贝到从上"><span class="nav-number">1.1.7.</span> <span class="nav-text">7.把主上的三个配置文件拷贝到从上</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#8-从上（slave）编辑-ha-cf"><span class="nav-number">1.1.8.</span> <span class="nav-text">8.从上（slave）编辑 ha.cf</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#9-启动-heartbeat"><span class="nav-number">1.1.9.</span> <span class="nav-text">9.启动 heartbeat</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#10-检查测试"><span class="nav-number">1.1.10.</span> <span class="nav-text">10.检查测试</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#11-测试1"><span class="nav-number">1.1.11.</span> <span class="nav-text">11.测试1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#12-测试2"><span class="nav-number">1.1.12.</span> <span class="nav-text">12.测试2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#13-测试脑裂"><span class="nav-number">1.1.13.</span> <span class="nav-text">13.测试脑裂</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
